//
//  LikedVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "LikedVC.h"
#import "XYuser.h"
#import "LikedCell.h"
#import "AttentionDetailVC.h"

@interface LikedVC ()
{
    int pIntPage;
}

@property(nonatomic, strong) NSMutableArray *pDataDicArr;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation LikedVC

- (void)initInfo
{
    [super initInfo];
    _pDataDicArr = [NSMutableArray array];
    pIntPage = 1;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"喜欢"];
    [self.pTableView setTableFooterView:[UIView new]];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pIntPage = 1;
        [weakSelf requestDataDicDown];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    [self.pTableView.mj_footer setHidden:YES];
}

- (void)requestDataDicDown
{
    __weak typeof(self) weakSelf = self;

    [TDNetwork requestLikeListPostUrl:SocialLikeListIP andPage:pIntPage andPageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [weakSelf.pDataDicArr removeAllObjects];
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"list"];
            NSLog(@"附近用户信息： %@", dictionary);
            for (NSDictionary *dic in arr)
            {
                XYuser *xyuser = [[XYuser alloc] init];
                
                xyuser.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                xyuser.beReportedCount  = [NSString stringWithFormat:@"%@", [dic valueForKey:@"beReportedCount"]];
                xyuser.breakCount       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                xyuser.distance         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"distance"]];
                xyuser.faceScore        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                xyuser.fans             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                xyuser.headImage        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                xyuser.movieLabel       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                xyuser.nickname         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                xyuser.phone            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                xyuser.pwd              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                xyuser.regTime          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"regTime"]];
                xyuser.saltCode         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"saltCode"]];
                xyuser.uid              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                xyuser.userAge          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                xyuser.userSex          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                xyuser.zambia           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                xyuser.address          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                xyuser.albumNum         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"albumNum"]];
                xyuser.charmValue       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"charmValue"]];
                xyuser.commentCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                xyuser.commentStart     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                xyuser.commentTotalScore= [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                xyuser.type             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                xyuser.activeTime       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                
                [_pDataDicArr addObject:xyuser];
            }
            
            [weakSelf.pTableView reloadData];
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
            
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
    }];
}

- (void)requestDataDic
{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestLikeListPostUrl:SocialLikeListIP andPage:pIntPage andPageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {

            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            if (pIntPage == 1) {
                [_pDataDicArr removeAllObjects];
                [self.pTableView reloadData];
            }
            
            NSArray *arr = [dict valueForKey:@"list"];
            NSLog(@"附近用户信息： %@", dictionary);
            for (NSDictionary *dic in arr)
            {
                XYuser *xyuser = [[XYuser alloc] init];
                
                xyuser.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                xyuser.beReportedCount  = [NSString stringWithFormat:@"%@", [dic valueForKey:@"beReportedCount"]];
                xyuser.breakCount       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                xyuser.distance         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"distance"]];
                xyuser.faceScore        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                xyuser.fans             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                xyuser.headImage        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                xyuser.movieLabel       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                xyuser.nickname         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                xyuser.phone            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                xyuser.pwd              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                xyuser.regTime          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"regTime"]];
                xyuser.saltCode         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"saltCode"]];
                xyuser.uid              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                xyuser.userAge          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                xyuser.userSex          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                xyuser.zambia           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                xyuser.address          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                xyuser.albumNum         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"albumNum"]];
                xyuser.charmValue       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"charmValue"]];
                xyuser.commentCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                xyuser.commentStart     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                xyuser.commentTotalScore= [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                xyuser.type             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                xyuser.activeTime       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                
                [_pDataDicArr addObject:xyuser];
            }
            
            [weakSelf.pTableView reloadData];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
            
        }
        
        [self.pTableView.mj_footer endRefreshing];
        [self.pTableView.mj_header endRefreshing];
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
    }];
}

- (void)requestCollection:(NSString *)uid sender:(UIButton *)sender
{
    [SVProgressHUD show];
    [TDNetwork requestCollection:MY_COLLECTION_List toUid:[uid integerValue] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showImage:[UIImage imageNamed:@"Attention_success"] status:@"成功关注" maskType:SVProgressHUDMaskTypeBlack];
//            [sender setImage:[UIImage imageNamed:@"social_attentioned"] forState:UIControlStateNormal];
//            [sender removeTarget:self action:@selector(btnCollectionAction:) forControlEvents:UIControlEventTouchUpInside];
//            [sender addTarget:self action:@selector(btnCancelCollectionAction:) forControlEvents:UIControlEventTouchUpInside];
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_ADD_ATTENTION object:nil];
            [self requestDataDic];
        } else {
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)requestCancelCollection:(NSString *)uid sender:(UIButton *)sender
{
    [SVProgressHUD show];
    [TDNetwork requestDeleteCollection:deleteCollection_interface toUid:uid complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showImage:[UIImage imageNamed:@"Attention_cancel"] status:@"已取消关注" maskType:SVProgressHUDMaskTypeBlack];
            [sender setImage:[UIImage imageNamed:@"social_addAttention"] forState:UIControlStateNormal];
            [sender removeTarget:self action:@selector(btnCancelCollectionAction:) forControlEvents:UIControlEventTouchUpInside];
            [sender addTarget:self action:@selector(btnCollectionAction:) forControlEvents:UIControlEventTouchUpInside];
        } else {
            [SVProgressHUD dismiss];
        }
    }];

}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pDataDicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LikedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfLikedCell"];
    XYuser *user = [_pDataDicArr objectAtIndex:indexPath.row];
    
    [cell.likeImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/api/common/file/imgGet?fid=%@", IP, user.headImage]]];
    [cell.likeImageView.layer setCornerRadius:cell.likeImageView.frame.size.width / 2];
    [cell.likeImageView.layer setMasksToBounds:YES];
    [cell.likedNameLB setText:user.nickname];
    [cell.regTimeLB setText:user.activeTimeLast];
    [cell.addressLB setText:user.address];
    [cell.ageLB setText:user.userAge];
    
    if ([user.userSex isEqualToString:@"1"]) {
        [cell.sexImageView setImage:[UIImage imageNamed:@"sex_boy"]];
    } else {
        [cell.sexImageView setImage:[UIImage imageNamed:@"sex_girl"]];
    }
    
    cell.collectionBTN.accessibilityIdentifier = user.uid;
    [cell.collectionBTN addTarget:self action:@selector(btnCollectionAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XYuser *user = [_pDataDicArr objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"idfAttentionDetailVC1" sender:user.uid];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)btnCollectionAction:(UIButton *)sender
{
    NSString *uid = sender.accessibilityIdentifier;
    if (uid.length > 0) {
        [self requestCollection:uid sender:sender];
    }
}

- (void)btnCancelCollectionAction:(UIButton *)sender
{
    NSString *uid = sender.accessibilityIdentifier;
    if (uid.length > 0) {
        [self requestCancelCollection:uid sender:sender];
    }
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfAttentionDetailVC1"]) {
        AttentionDetailVC *nextVC = segue.destinationViewController;
        nextVC.hiddenAttentionBtn = YES;
        nextVC.strID = sender;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
